﻿using AppDataStruct;
using ComLib.ConfigService;
using ComUIControl;
using KWindow.KPageCore;
using LogLib;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace YSLPages
{
    public partial class PageSelectReceiveWay : PageControlBase
    {     
        private Image imgEMSCheck = null;

        private Image imgEMS = null;

        private Image imgPCSLQCheck = null;

        private Image imgPCSLQ = null;

        private Button buttonPoliceOffice = null;

        private Button buttonPostOffice = null;

        private TextBox textBoxSJRXM = null;

        private TextBox textBoxLXDH = null;

        private TextBox textBoxYZBM = null;

        private TextBox textBoxSJDZ = null;

        private TextBox textBoxJDDZ = null;

        //private TextBox textBoxPoliceOfficeName = null;

        private PreApplyInfo _appInfo = null;

        private List<Control> listEMSInfoControls = new List<Control>();

        private List<Control> listPCSInfoControls = new List<Control>();
        private bool _bInited = false;
        public PageSelectReceiveWay()
        {
            InitializeComponent();
            SetProcessBase(ProcessSelectReceiveWay.Instance);
        }

        private void GetControls()
        {
            imgPCSLQ = GetImage("PCSLQ");
            imgPCSLQCheck = GetImage("PCSLQ_Check");
            imgEMS = GetImage("EMS");
            imgEMSCheck = GetImage("EMS_Check");
            buttonPoliceOffice = GetControl<Button>("buttonPoliceOffice") as Button;
            buttonPostOffice = GetControl<Button>("buttonPostOffice") as Button;

            textBoxSJRXM = GetControl<TextBox>("textBoxSJRXM") as TextBox;
            textBoxYZBM = GetControl<TextBox>("textBoxYZBM") as TextBox;
            textBoxSJDZ = GetControl<TextBox>("textBoxSJDZ") as TextBox;
            textBoxJDDZ = GetControl<TextBox>("textBoxJDDZ") as TextBox;
            textBoxLXDH = GetControl<TextBox>("textBoxLXDH") as TextBox;
            //textBoxPoliceOfficeName = GetControl<TextBox>("textBoxPoliceOfficeName") as TextBox;

            SetButtonDefaultStyles(buttonPoliceOffice);
            SetButtonDefaultStyles(buttonPostOffice);
        }

        //初始化窗体数据
        public override void InitCtrls()
        {
            InitKeyboards();
            base.InitCtrls();
            GetControls();
            listEMSInfoControls = dicSubGroup[buttonPostOffice];
            listPCSInfoControls = dicSubGroup[buttonPoliceOffice];
            XZQHService.Init();         
        }

        private void InitButtonImage()
        {
            if (this.InvokeRequired)
            {
                Action handler = new Action(InitButtonImage);
                this.Invoke(handler);
                return;
            }
            if (_appInfo.QZFS.Code == "02")
            {
                buttonPoliceOffice.Image = imgPCSLQ;
                buttonPostOffice.Image = imgEMSCheck;
                ShowPCSInfoControls(false);
                ShowEMSInfoControls(true);               
            }
            else if(_appInfo.QZFS.Value == "01")
            {
                buttonPoliceOffice.Image = imgPCSLQCheck;
                buttonPostOffice.Image = imgEMS;
                ShowEMSInfoControls(false);
                ShowPCSInfoControls(true);
            }
            else
            {
                buttonPoliceOffice.Image = imgPCSLQ;
                buttonPostOffice.Image = imgEMS;
                ShowEMSInfoControls(false);
                ShowPCSInfoControls(false);
            }          
        }

        private void InitTextBoxs()
        {
            if(this.InvokeRequired)
            {
                Action handler = new Action(InitTextBoxs);
                this.Invoke(handler);
                return;
            }
            if (string.IsNullOrEmpty(_appInfo.SJR))
            {
                textBoxSJRXM.Text = _appInfo.ZWXM;
            }
            else
            {
                textBoxSJRXM.Text = _appInfo.SJR;
            }
            if (string.IsNullOrEmpty(_appInfo.SJRLXDH))
            {
                textBoxLXDH.Text = _appInfo.LXDH;
            }
            else
            {
                textBoxLXDH.Text = _appInfo.SJRLXDH;
            }
            textBoxYZBM.Text = _appInfo.YB;
            if(!string.IsNullOrEmpty(_appInfo.YJDZXZQH))
            {
                textBoxSJDZ.Text = XZQHService.GetDZName(_appInfo.YJDZXZQH);
            }         
            else
            {
                textBoxSJDZ.Text = _appInfo.HKSZD.Value;
                _appInfo.YJDZXZQH = _appInfo.HKSZD.Code;
            }
            if(!string.IsNullOrEmpty(_appInfo.QZFS.Code))
            {
                if (_appInfo.QZFS.Code == "01")
                {
                    textBoxJDDZ.Text = _appInfo.YJDZ.Substring(textBoxSJDZ.Text.Length);
                }
                else
                {
                    textBoxJDDZ.Text = _appInfo.YJDZ.Substring(textBoxSJDZ.Text.Length);
                }
            }           
        }

        private bool UpdateQZFSCode()
        {
            if(buttonPoliceOffice.Image == imgPCSLQCheck 
                && buttonPostOffice.Image == imgEMS)
            {
                _appInfo.QZFS.Code =  "01";
                _appInfo.QZFS.Value = "受理点领取";
                return true;
            }
            if (buttonPostOffice.Image == imgEMSCheck
                && buttonPoliceOffice.Image == imgPCSLQ)
            {
                _appInfo.QZFS.Code = "02";
                _appInfo.QZFS.Value = "邮寄送达";
                return true;
            }
            MsgBox.Show("没有选择有效的取证方式.");
            return false;
        }

        /// <summary>
        /// 更新邮寄信息
        /// </summary>
        /// <returns></returns>
        private bool UpdateEMSInfo()
        {
            if(string.IsNullOrEmpty(textBoxSJRXM.Text))
            {
                MsgBox.Show("收件人不能为空.");
                return false;
            }
            if (string.IsNullOrEmpty(textBoxLXDH.Text))
            {
                MsgBox.Show("收件人联系电话不能为空.");
                return false;
            }
            if (string.IsNullOrEmpty(textBoxSJDZ.Text))
            {
                MsgBox.Show("收件地址省市县不能为空.");
                return false;
            }
            string dzInfo = "";
            if(_appInfo.QZFS.Code == "01")  //受理点取证
            {
                //if (string.IsNullOrEmpty(textBoxPoliceOfficeName.Text))
                //{
                //    MessageBox.Show("派出所名称不能为空.");
                //    return false;
                //}
                //else
                //{
                //    dzInfo = textBoxPoliceOfficeName.Text;
                //}
            }
            if (_appInfo.QZFS.Code == "02")   //快递
            {
                if (string.IsNullOrEmpty(textBoxJDDZ.Text))
                {
                    MessageBox.Show("收件街道详细地址不能为空.");
                    return false;
                }
                else
                {
                    dzInfo = textBoxJDDZ.Text;
                    _appInfo.SJR = textBoxSJRXM.Text;
                    _appInfo.SJRLXDH = textBoxLXDH.Text;
                    _appInfo.YB = textBoxYZBM.Text;
                    _appInfo.YJDZ = textBoxSJDZ.Text + dzInfo;
                    _appInfo.YJDZXZQH = textMapService["SJDZ"];
                }
            }

            textMapService.UpdateMapDic();
           
            return true;
        }

        //初始化窗体数据
        public override void InitData()
        {
            base.InitData();
            _bInited = false;
            _appInfo = Process.GetValue<PreApplyInfo>("PreApplyInfo");          
            if(_appInfo != null)
            {
                InitTextBoxs();
            }          
            InitButtonImage();
            _bInited = true;
            Speak("请选择取证方式");
        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            Process.SetValue("PreApplyInfo", _appInfo);
        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
            for(int i=0; i<TextBoxs.Count; i++)
            {
                TextBoxs[i].Text = "";
            }
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
            base.Close();
        }

        public override void StartWork()
        {
            base.StartWork();
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        public override void OnCountdownCompleted(object sender, EventArgs e)
        {
            Log.WriteAsync("倒计时结束，用户没有有效操作，中止流程");
            ClearData();
            Process.ToHome();
        }

        protected override void btnBack_Click(object sender, EventArgs e)
        {
            StopCountdown();
            base.btnBack_Click(sender, e);
        }

        protected override void btnNext_Click(object sender, EventArgs e)
        {
            StopCountdown();
            if (!UpdateQZFSCode())
            {
                return;
            }
            if (!UpdateEMSInfo())
            {
                return;
            }
            base.btnNext_Click(sender, e);
        }

        protected override void btnCancel_Click(object sender, EventArgs e)
        {
            StopCountdown();
            base.btnCancel_Click(sender, e);
        }

        private void ShowEMSInfoControls(bool showing)
        {
            for(int i=0; i<listEMSInfoControls.Count; i++)
            {
                listEMSInfoControls[i].Visible = showing;
            }          
        }

        private void ShowPCSInfoControls(bool showing)
        {
            if (listPCSInfoControls == null)
                return;
            for (int i = 0; i < listPCSInfoControls.Count; i++)
            {
                listPCSInfoControls[i].Visible = showing;
            }
        }

        private void btnYJ_Click(object sender, EventArgs e)
        {
            if (buttonPostOffice.Image == imgEMS)
            {
                buttonPostOffice.Image = imgEMSCheck;
                buttonPoliceOffice.Image = imgPCSLQ;
                ShowPCSInfoControls(false);
                ShowEMSInfoControls(true);              
                if (string.IsNullOrEmpty(textBoxJDDZ.Text) && !string.IsNullOrEmpty(_appInfo.YJDZ))
                {
                    textBoxJDDZ.Text = _appInfo.YJDZ.Substring(textBoxSJDZ.Text.Length);
                }     
            }
        }

        private void btnPCS_Click(object sender, EventArgs e)
        {
            if (buttonPoliceOffice.Image == imgPCSLQ)
            {
                buttonPoliceOffice.Image = imgPCSLQCheck;
                buttonPostOffice.Image = imgEMS;
                ShowEMSInfoControls(false);
                ShowPCSInfoControls(true);
            }
        }
    }
}
